package com.example.exp_7;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.net.Uri;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

public class MyProvider extends ContentProvider {
    private static final String AUTHORITY = "com.example.exp_7.myprovider";
    private static UriMatcher uriMatcher;

    private SQLHelper sqlHelper;
    private SQLiteDatabase db;
    static {
        uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
        uriMatcher.addURI(AUTHORITY,"/#",1);
    }
    @Override
    public boolean onCreate() {
        sqlHelper = new SQLHelper(this.getContext(),"staff.db",null,1);

        uriMatcher.addURI(AUTHORITY,"search",1);
        uriMatcher.addURI(AUTHORITY,"delete",1);

        return true;
    }

    @Nullable
    @Override
    public Cursor query(@NonNull Uri uri, @Nullable String[] strings, @Nullable String s, @Nullable String[] strings1, @Nullable String s1) {
        Cursor cursor = null;
        db = sqlHelper.getReadableDatabase();
        switch (uriMatcher.match(uri)) {
            case 1:
                cursor = db.query("staff",strings,s,strings1,null,null,null);
                break;
            default:
                Toast.makeText(this.getContext(),"未知uri",Toast.LENGTH_SHORT).show();
        }
        return cursor;
    }

    @Nullable
    @Override
    public String getType(@NonNull Uri uri) {
        return null;
    }

    @Nullable
    @Override
    public Uri insert(@NonNull Uri uri, @Nullable ContentValues contentValues) {
        return null;
    }

    @Override
    public int delete(@NonNull Uri uri, @Nullable String s, @Nullable String[] strings) {
        db = sqlHelper.getReadableDatabase();
        switch (uriMatcher.match(uri)) {
            case 1:
                db.delete("staff",s,strings);
                break;
            default:
                Toast.makeText(this.getContext(),"未知uri",Toast.LENGTH_SHORT).show();
        }
        return 0;
    }
    @Override
    public int update(@NonNull Uri uri, @Nullable ContentValues contentValues, @Nullable String s, @Nullable String[] strings) {
        return 0;
    }
}
